package com.jay.net.clazz.socket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

class TCPServer implements Runnable {
	private ServerSocket serverSocket;
	private ConnectionHandler handler;

	// constructor
	public TCPServer(int port, ConnectionHandler handler) throws IOException {
		this.serverSocket = new ServerSocket(port);
		this.handler = handler;
	}

	public void run() {
		for (;;) {
			try {
				Socket socket = serverSocket.accept();
				// 不用线程的话，会阻塞，从而只能一个一个client的处理，无法做到并行
				// new ConnectionHandler(socket).run();
				new Thread(handler.setSocket(socket)).start();
			} catch (IOException e) { /* … */
			}
		} // end finally
	} // end run()
} // end class
